import { useState, useEffect } from "react";
import { format, isToday } from "date-fns";
import { zhCN } from "date-fns/locale"; // 导入中文 locale

interface DateDisplay {
  formattedDate: string;
  formattedTime: string;
  formattedDayOfWeek: string;
  isToday: boolean;
}

const useDateDisplay = (): DateDisplay => {
  const [currentDate, setCurrentDate] = useState<Date>(new Date());

  useEffect(() => {
    const timer = setInterval(() => {
      setCurrentDate(new Date());
    }, 1000);

    return () => clearInterval(timer);
  }, []);

  const formattedDate = format(currentDate, "yyyy年M月d日", { locale: zhCN });
  const formattedDayOfWeek = format(currentDate, "EEEE", { locale: zhCN });
  const isTodayValue = isToday(currentDate);

  // 12小时制格式化时间
  // const formattedTime12 = format(currentDate, 'h:mm a', { locale: zhCN });

  // 24小时制格式化时间
  const formattedTime24 = format(currentDate, "HH:mm", { locale: zhCN });

  return {
    formattedDate,
    formattedTime: formattedTime24, // 默认使用24小时制时间
    formattedDayOfWeek,
    isToday: isTodayValue,
  };
};

export default useDateDisplay;
